import {
  createRouter,
  createWebHashHistory,
} from "vue-router"
import type { RouteRecordRaw } from "vue-router"
import localCach from "@/api/localCach"

const routes: RouteRecordRaw[] = [
  {
    path: "/",
    redirect: "/main",
  },
  {
    path: "/login",
    component: () => import("@/views/login/login.vue"),
  },
  {
    path: "/main",
    component: () => import("@/views/main/main.vue"),
    name: "main",
    // children: [
    //   {
    //     path: "product/goods",
    //     component: () => import("@/views/main/system/goods/goods.vue"),
    //     // component: () => import("@/views/login/login.vue"),
    //   },
    // ],
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import("@/components/error-page/error-page.vue")
  }
]

const router = createRouter({
  routes,
  history: createWebHashHistory(),
})



// 全局前置路由守卫
router.beforeEach((to) => {
  // 验证用户是否登录
  if (to.path !== "/login" ) {
    const token = localCach.getCach("token")
    if (!token) {
      return "/login"
    }
  }
})


export default router
